var t = require("../../@babel/runtime/helpers/interopRequireDefault"), e = require("../../@babel/runtime/helpers/createForOfIteratorHelper"), a = t(require("../../@babel/runtime/regenerator")), r = require("../../@babel/runtime/helpers/asyncToGenerator"), i = t(require("../../http/order")), s = t(require("../../http/offlineShop")), o = t(require("../../http/robot")), n = t(require("../../http/system")), l = require("../../utils/tool"), u = require("../../behavior/pagination.js"), d = getApp(), c = new i.default(), h = new s.default(), p = new o.default(), f = new n.default();

Page({
    behaviors: [ u.paginationBev ],
    data: {
        titleBarTotalHeight: d.global.systemInfo.titleBarTotalHeight,
        screenHeight: d.global.systemInfo.screenHeight,
        safeAreaHeight: d.global.systemInfo.safeAreaHeight,
        typeTabHeight: 0,
        statusTabHeight: 0,
        expireHeiht: 0,
        offset: 37.5,
        orderType: 0,
        orderStatus: 20,
        expireTag: 0,
        orderStatusList: [ {
            label: "盒柜",
            value: 20
        }, {
            label: "待发货",
            value: 30
        }, {
            label: "待收货",
            value: 40
        }, {
            label: "已完成",
            value: 50
        } ],
        selectList: [],
        show: !1,
        totalCount: 0,
        hasExpire: 0,
        showType: !0,
        scrollTop: 0,
        isAll: !1,
        expireFlag: !1,
        canSelectCount: 0
    },
    onLoad: function(t) {
        var e = parseInt(t.orderType) || 0, a = parseInt(t.orderStatus) || 20;
        this.setData({
            orderType: e,
            orderStatus: a,
            offset: e ? 37.5 * (e + 1) + 200 * e : 37.5
        }), this.getHeight(), this.getList(), this.getExpire();
    },
    onShow: function() {
        this.selectComponent("#apply").data.addressFlag && this.selectComponent("#apply").getAddressList();
    },
    getList: function() {
        var t = this;
        return r(a.default.mark(function e() {
            var r;
            return a.default.wrap(function(e) {
                for (;;) switch (e.prev = e.next) {
                  case 0:
                    try {
                        t.loading(), 0 == (r = t.data.orderType) ? t.getMiniList(1) : 1 == r ? t.getRobotList(1) : t.getShopList(1);
                    } catch (e) {
                        t.stopLoad();
                    }

                  case 1:
                  case "end":
                    return e.stop();
                }
            }, e);
        }))();
    },
    refreshList: function() {
        this.setData({
            dataArray: [],
            selectList: [],
            isAll: !1,
            totalCount: 0
        }), this.getList();
    },
    loadMore: function() {
        var t = this;
        return r(a.default.mark(function e() {
            var r;
            return a.default.wrap(function(e) {
                for (;;) switch (e.prev = e.next) {
                  case 0:
                    if (e.prev = 0, !t.isLoading()) {
                        e.next = 3;
                        break;
                    }
                    return e.abrupt("return");

                  case 3:
                    t.hasMore() && (t.loading(), 0 == (r = t.data.orderType) ? t.getMiniList(t.getPageNum()) : 1 == r ? t.getRobotList(t.getPageNum()) : t.getShopList(t.getPageNum())), 
                    e.next = 10;
                    break;

                  case 6:
                    e.prev = 6, e.t0 = e.catch(0), (0, l.toastToPage)(e.t0), t.stopLoad();

                  case 10:
                  case "end":
                    return e.stop();
                }
            }, e, null, [ [ 0, 6 ] ]);
        }))();
    },
    showExpire: function() {
        this.setData({
            expireTag: this.data.expireTag ? 0 : 1,
            selectList: [],
            dataArray: [],
            isAll: !1,
            totalCount: 0,
            canSelectCount: 0
        }), this.getList();
    },
    getMiniList: function(t) {
        var i = this;
        c.getOrdersList({
            pageNum: t,
            pageSize: this.data.pageSize,
            orderStatus: this.data.orderStatus,
            expireTag: this.data.expireTag
        }).then(function() {
            var t = r(a.default.mark(function t(r) {
                var s, o, n, u, d, c, h, p, g, y;
                return a.default.wrap(function(t) {
                    for (;;) switch (t.prev = t.next) {
                      case 0:
                        return t.next = 2, f.getServerTime();

                      case 2:
                        s = t.sent, 20 == i.data.orderStatus && 0 != r.data.list.length && 0 == i.data.expireTag && i.setData({
                            expireFlag: !0
                        }), o = e(r.data.list);
                        try {
                            for (o.s(); !(n = o.n()).done; ) {
                                (u = n.value).select = 0, u.total = u.orderTotalAmount.toString().split("."), u.discount = (u.orderTotalAmount - u.paymentAmount).toFixed(2).toString().split("."), 
                                u.actual = u.paymentAmount.toString().split("."), d = u.orderItemVoList[0].estimatedDeliveryTime, 
                                c = e(u.orderItemVoList);
                                try {
                                    for (c.s(); !(h = c.n()).done; ) (p = h.value).isEstimatedDelivery = d && (0, l.isMoreThanNow)(d), 
                                    p.estimatedDeliveryTime = d ? d.substring(0, 10) : "", p.name = p.ipEnName ? p.ipEnName + p.productName : "潮玩公社" != p.ipCnName && p.ipCnName ? p.ipCnName + p.productName : p.productName;
                                } catch (t) {
                                    c.e(t);
                                } finally {
                                    c.f();
                                }
                                u.autoOrderTime && (g = (g = (0, l.getDifferentDays)(u.autoOrderTime.substring(0, 10), s.data.systemDate.substring(0, 10)).toFixed(0)).startsWith("-") || g.startsWith("0") ? null : g, 
                                u.remainingTime = g);
                            }
                        } catch (t) {
                            o.e(t);
                        } finally {
                            o.f();
                        }
                        y = r.data.list.filter(function(t) {
                            return !t.orderItemVoList[0].isEstimatedDelivery;
                        }), i.setData({
                            canSelectCount: i.data.canSelectCount + y.length
                        }), i.setMoreData(r.data.list), i.setTotal(r.data.totalCount, r.data.currPage), 
                        i.stopLoad();

                      case 11:
                      case "end":
                        return t.stop();
                    }
                }, t);
            }));
            return function(e) {
                return t.apply(this, arguments);
            };
        }());
    },
    getRobotList: function(t) {
        var a = this;
        p.robotOrderList({
            pageNum: t,
            pageSize: this.data.pageSize
        }).then(function(t) {
            var r, i = e(t.data.list);
            try {
                for (i.s(); !(r = i.n()).done; ) {
                    var s = r.value;
                    s.total = s.totalAmount.toString().split("."), s.discount = (s.totalAmount - s.paymentAmount).toString().split("."), 
                    s.actual = s.paymentAmount.toString().split(".");
                    var o, n = [], l = 0, u = e(s.orderItemList);
                    try {
                        var d = function() {
                            var t = o.value;
                            l += t.quantity;
                            var e = n.findIndex(function(e) {
                                return e.productNo == t.productNo;
                            }), a = 0;
                            -1 == e ? (1 == t.refundStatus && (a = 1), n.push({
                                productNo: t.productNo,
                                productTitle: t.productTitle,
                                quantity: t.quantity,
                                mallPrice: t.mallPrice,
                                refundCount: a
                            })) : "sale" == t.productType ? (n[e].quantity++, 1 == t.refundStatus && n[e].refundCount++) : (1 == t.refundStatus && (a = 1), 
                            n.push({
                                productNo: t.productNo,
                                productTitle: "gift" == t.productType ? "[赠品]".concat(t.productTitle) : t.productTitle,
                                quantity: t.quantity,
                                mallPrice: t.mallPrice,
                                refundCount: a
                            }));
                        };
                        for (u.s(); !(o = u.n()).done; ) d();
                    } catch (t) {
                        u.e(t);
                    } finally {
                        u.f();
                    }
                    s.goodsList = n, s.goodsCount = l;
                }
            } catch (t) {
                i.e(t);
            } finally {
                i.f();
            }
            a.setMoreData(t.data.list), a.setTotal(t.data.totalCount, t.data.currPage), a.stopLoad();
        });
    },
    getShopList: function(t) {
        var a = this;
        h.offlineShopOrderList({
            pageNum: t,
            pageSize: this.data.pageSize
        }).then(function(t) {
            var r, i = e(t.data.list);
            try {
                for (i.s(); !(r = i.n()).done; ) {
                    var s = r.value;
                    s.select = 0, s.total = s.orderTotalAmount.toString().split("."), s.discount = (s.orderTotalAmount - s.paymentAmount).toString().split("."), 
                    s.actual = s.paymentAmount.toString().split(".");
                    var o, n = [], l = 0, u = e(s.orderItemVoList);
                    try {
                        for (u.s(); !(o = u.n()).done; ) {
                            var d = o.value;
                            l += d.quantity, n.push({
                                productNo: d.productNo,
                                productTitle: d.productName,
                                quantity: d.quantity,
                                mallPrice: d.mallPrice,
                                refundCount: d.refundQuantity
                            });
                        }
                    } catch (t) {
                        u.e(t);
                    } finally {
                        u.f();
                    }
                    s.goodsList = n, s.goodsCount = l;
                }
            } catch (t) {
                i.e(t);
            } finally {
                i.f();
            }
            a.setMoreData(t.data.list), a.setTotal(t.data.totalCount, t.data.currPage), a.stopLoad();
        });
    },
    changeOrderType: function(t) {
        var e = parseInt(t.currentTarget.dataset.index);
        this.data.orderType != e && (0 == e ? (this.setData({
            offset: 37.5,
            orderType: e
        }), this.selectComponent("#tabs").resize()) : this.setData({
            offset: 37.5 * (e + 1) + 200 * e,
            orderType: e
        }), this.setData({
            isAll: !1,
            pageNum: 1,
            dataArray: [],
            selectList: [],
            expireTag: 0,
            showType: !0
        }), this.getHeight(), this.getList());
    },
    changeOrderStatus: function(t) {
        wx.pageScrollTo({
            scrollTop: 0,
            duration: 300
        }), this.setData({
            orderStatus: t.detail.name,
            isAll: !1,
            pageNum: 1,
            dataArray: [],
            selectList: [],
            expireTag: 0,
            showType: !0
        }), this.getHeight(), this.getList();
    },
    selectOrder: function(t) {
        var a = t.detail.select, r = t.detail.orderId, i = this.data.selectList, s = 0, o = this.data.dataArray, n = this.data.dataArray.findIndex(function(t) {
            return t.orderId == r;
        });
        o[n].select = a;
        var l, u = e(o[n].orderItemVoList);
        try {
            for (u.s(); !(l = u.n()).done; ) {
                s += l.value.quantity;
            }
        } catch (t) {
            u.e(t);
        } finally {
            u.f();
        }
        if (a) i.push(r), 0 != i.length && i.length == this.data.canSelectCount && this.setData({
            isAll: !0
        }), this.setData({
            totalCount: this.data.totalCount + s
        }); else {
            var d = i.findIndex(function(t) {
                return t == r;
            });
            i.splice(d, 1), this.setData({
                isAll: !1,
                totalCount: this.data.totalCount - s
            });
        }
        this.setData({
            dataArray: o,
            selectList: i
        });
    },
    selectAll: function() {
        if (0 != this.data.dataArray.length && 0 != this.data.canSelectCount) {
            var t = 0;
            if (this.data.isAll) {
                var a, r = this.data.dataArray, i = e(r);
                try {
                    for (i.s(); !(a = i.n()).done; ) {
                        a.value.select = !1;
                    }
                } catch (t) {
                    i.e(t);
                } finally {
                    i.f();
                }
                this.setData({
                    selectList: [],
                    dataArray: r,
                    isAll: !1,
                    totalCount: 0
                });
            } else {
                var s, o = this.data.dataArray, n = [], l = e(o);
                try {
                    for (l.s(); !(s = l.n()).done; ) {
                        var u = s.value;
                        u.orderItemVoList[0].isEstimatedDelivery || (u.select = !0, n.push(u.orderId), t += u.orderItemVoList[0].quantity);
                    }
                } catch (t) {
                    l.e(t);
                } finally {
                    l.f();
                }
                this.setData({
                    selectList: n,
                    dataArray: o,
                    isAll: !0,
                    totalCount: t
                });
            }
        }
    },
    showApply: function() {
        0 != this.data.selectList.length ? this.selectComponent("#apply").getDefaultAddress() : (0, 
        l.toastToPage)("请先选择订单");
    },
    showGet: function(t) {
        this.setData({
            show: !0,
            comfirmId: t.detail.orderId
        });
    },
    hideGet: function() {
        this.setData({
            show: !1
        });
    },
    confirmGet: function(t) {
        var e = this;
        return r(a.default.mark(function t() {
            return a.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    wx.showLoading({
                        title: "加载中",
                        mask: !0
                    }), c.confirmReceipt(e.data.comfirmId).then(function(t) {
                        e.setData({
                            show: !1
                        }), wx.hideLoading(), (0, l.toastToPage)("确认收货成功"), e.setData({
                            orderStatus: e.data.orderStatus,
                            isAll: !1,
                            pageNum: 1,
                            dataArray: [],
                            selectList: []
                        }), e.getList();
                    }).catch(function(t) {
                        wx.hideLoading();
                    });

                  case 2:
                  case "end":
                    return t.stop();
                }
            }, t);
        }))();
    },
    getExpire: function() {
        var t = this;
        c.getExpire().then(function(e) {
            t.setData({
                hasExpire: e.data.count
            });
        }).catch(function(t) {});
    },
    showTypeTab: function() {
        this.setData({
            showType: !0
        }), this.getHeight();
    },
    scrollHide: (0, l.throttling)(function(t) {
        0 == this.data.orderType && this.data.dataArray.length > 0 && t.detail.scrollTop > this.data.typeTabHeight && this.setData({
            showType: !1,
            typeTabHeight: 0
        });
    }, 100),
    getHeight: function() {
        var t = this, e = wx.createSelectorQuery();
        e.select("#typeTab").boundingClientRect(), e.exec(function(e) {
            t.setData({
                typeTabHeight: e[0] && t.data.showType ? e[0].height : 0
            });
        });
        var a = wx.createSelectorQuery();
        a.select("#statusTab").boundingClientRect(), a.exec(function(e) {
            t.setData({
                statusTabHeight: e[0].height
            });
        });
        var r = wx.createSelectorQuery();
        r.select("#expire").boundingClientRect(), r.exec(function(e) {
            t.setData({
                expireHeiht: e[0] ? e[0].height : 0
            });
        });
    },
    toIndex: function() {
        wx.switchTab({
            url: "/pages/index/index"
        });
    }
});